package cn.edu.gzccc.movie.Controller;

import cn.edu.gzccc.movie.domain.Actor;
import cn.edu.gzccc.movie.domain.Movie;
import cn.edu.gzccc.movie.domain.Role;
import cn.edu.gzccc.movie.repository.ActorRepository;
import cn.edu.gzccc.movie.repository.MovieRepository;
import cn.edu.gzccc.movie.repository.RoleRepository;
import cn.edu.gzccc.movie.service.ActorService;
import cn.edu.gzccc.movie.service.MovieService;
import cn.edu.gzccc.movie.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private MovieService movieService;

    @Autowired
    private ActorService actorService;

    @Autowired
    private RoleService roleService;

    /**
     * 跳转到新增角色页面
     *
     * @param model
     * @param movieId
     * @return
     */
    @RequestMapping(value = "/{movieId}/add", method = RequestMethod.GET)
    public String toAdd(Model model, @PathVariable Long movieId) {
        Movie movie = movieService.findOne(movieId);
        List<Actor> actors = actorService.findAll();
        model.addAttribute("movie", movie);
        model.addAttribute("actor", actors);
        return "/role/add";
    }

    /**
     * 保存新增角色信息并跳转到电影详情页面
     *
     * @param movieId
     * @param name
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@RequestParam Long movieId, @RequestParam String name, @RequestParam Long roleId,String introducer,String img) {
        roleService.add(movieId, roleId, name,introducer,img);
        return "redirect:/movie/detail/" + movieService.getMovieId(movieId);
    }

    /**
     * 删除当前角色并跳转到电影详情页面
     *
     * @param movieId
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/{movieId}/delete/{roleId}", method = RequestMethod.GET)
    public String delect(@PathVariable Long movieId, @PathVariable Long roleId) {
        roleService.delete(roleId);
        return "redirect:/movie/detail/" + movieService.getMovieId(movieId);
    }

    /**
     * 跳转到角色编辑页面
     *
     * @param model
     * @param roleId
     * @param movieId
     * @return
     */
    @RequestMapping(value = "/{movieId}/edit/{roleId}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long roleId, @PathVariable Long movieId) {
        Role role = roleService.findOne(roleId);
        List<Actor> actors = actorService.findAll();
        Movie movie = movieService.findOne(movieId);
        model.addAttribute("movie", movie);
        model.addAttribute("role", role);
        model.addAttribute("actors", actors);
        return "role/edit";
    }

    /**
     * 保存角色修改后的信息并跳转到电影详情页面
     *
     * @param roleId
     * @param name
     * @param actorId
     * @param movieId
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(@RequestParam Long roleId, @RequestParam String name, @RequestParam Long actorId, @RequestParam Long movieId,String introducer,String img) {
        roleService.edit(movieId, roleId, name, actorId,introducer,img);
        return "redirect:/movie/detail/" + movieService.getMovieId(movieId);
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable Long id, Model model) {
        Role role = roleService.findOne(id);
        model.addAttribute("role", role);
        return "/role/detail";
    }
}
